<?php
namespace Ng\Controller;
use Think\Controller;
class UserController extends Controller {
   /**
  * 查询用户信息分页数据
  * @param integer|string $page 分页页码
  * @param integer\string $pagesize 分页当前页显示的条数
  * @param string $name 名字,根据名字模糊查询
  * @return void 没有返回值
  * @method GET
    */
    public function get($currentPage=1,$pageSize=10,$name='',$id=0){
      \Think\Log::record('查询用户信息','INFO');
      if (IS_GET) {
        $model = M('user');
        $where = 'is_delete = 1 and user_name like "%'.$name.'%"';
        if ($id && $id != '') {
          $where = 'is_delete = 1 and id = "'.$id.'"';
        }
        $res = $model
        ->field('id, user_name, user_email, user_image, user_coins, user_rank, user_status, user_reg_time, user_motto')
        ->where($where)
        ->page($currentPage,$pageSize)
        ->select();
        if (!$id || $id == '') {
          $total = $model->where($where)->count();
        }
        $total = 1;
        if ($res) {
          $data['code'] = 200;
          $data['msg'] = '成功';
          $data['obj'] = $res;
          $data['total'] = $total;
          $this->ajaxReturn($data);
        } else {
          $data['code'] = 201;
          $data['msg'] = '查询失败';
          $this->ajaxReturn($data);
        }
      } else {
        $data['code'] = 400;
        $data['msg'] = '提交方式有误';
        $this->ajaxReturn($data);
      }
    }

  /**
  * 添加用户
  * 成功返回 用户信息 失败返回 提示信息
  * @param integer|string $username 用户名   不允许重复
  * @param integer|string $password 用户密码 
  * @param integer|string $repass   确认密码
  * @param integer|string $email    用户邮箱 不允许重复
  * @param integer|string $code     验证码   暂未开发
  * @return void 没有返回值
  * @method GET
  */
  public function createUser(){
    \Think\Log::record('创建用户信息','INFO');
    if (IS_POST) {
      $obj = getPost();
      $model = M('user');
      if ($model->where('user_name="'.$obj->username.'"')->count()) {
        $data['code']=201;
        $data['msg']='用户名已存在';
        $this->ajaxReturn($data);
      }
      if ($model->where('user_email="'.$obj->email.'"')->count()) {
        $data['code']=201;
        $data['msg']='邮箱已存在无法使用';
        $this->ajaxReturn($data);
      }
      if ($obj->password != $obj->repass) {
        $data['code']=201;
        $data['msg']='两次输入的密码不一致';
        $this->ajaxReturn($data);
      }
      $tmp['user_name'] = $obj->username;
      $tmp['user_email'] = $obj->email;
      // 密码加密
      $tmp['user_pwd'] = md5($obj->password);
      // 给用户添加一个默认头像
      $tmp['user_image'] = 'http://ajax.winnerwly.top/Public/images/avatar/'.rand(1,10).'.jpg';
      // 添加创建时间
      $tmp['user_reg_time'] = date('Y-m-d H:i:s');
              
      $res = $model->add($tmp);
      if ($res) {
        $userifno = $model
        ->field('id,user_name,user_email,user_image,user_coins,user_rank,user_status,user_reg_time,user_motto')
        ->where('id='.$res)
        ->select();
        if ($userifno) {
          $data['code']=200;
          $data['msg']='添加成功';
          $data['obj']=$userifno;
          $this->ajaxReturn($data);
        }
      } else {
        $data['code']=201;
        $data['msg']='添加失败';
        $this->ajaxReturn($data);
      }
    } else {
      $data['code']=400;
      $data['msg']='提交方式有误';
      $this->ajaxReturn($data);
    }
  }

  /**
  * 删除用户
  * 返回 提示信息
  * @param integer|string $ids id集合 不允许重复
  * @return void 没有返回值
  * @method DELETE
  */
  public function deleteUser(){
    \Think\Log::record('删除用户信息(逻辑删除)','INFO');
    if (IS_DELETE) {
      $model = M('user');
      $obj = getPost();
      if ($obj->ids) {
        $p = '/(((\d+)\s*,\s*)*)(\d+)/';
        preg_match($p, $obj->ids, $match);
        $tmp = explode(",",$match[0]); 
        $info['is_delete'] = 0;
        $res = $model->where('id in ('.$match[0].')')->save($info);

        if ($res) {
          $data['code']=200;
          $data['msg']='删除成功';
          $data['obj']=$res;
          $this->ajaxReturn($data);
        }else{
          $data['code']=201;
          $data['msg']='删除失败';
          $this->ajaxReturn($data);
        }
                
        // $arrlength=count($tmp);
        // $index = [];
        // for($i=0;$i<$arrlength;$i++) {
        //   $id = (int)$tmp[$i];
        //   $info['is_delete'] = 0;
        //   $res = $model->where("id={$id}")->save($info);
        //   $index[$i]['id'] = $id;
        //   $index[$i]['info'] = $res ? '删除成功' : '未修改';
        // }
        // $data['code'] = 200;
        // $data['msg'] = '删除成功';
        // $data['info'] = $index;
        // $this->ajaxReturn($data);
      } else {
        $data['code']=201;
        $data['msg']='没有传入正确的参数';
      }
    } else {
      $data['code']=400;
      $data['msg']='提交方式有误';
      $this->ajaxReturn($data);
    }
  }
}